In [1]:
import sys
sys.path.append('../src/mane/prototype/')
import numpy as np
import graph as g
import pickle as p
from sklearn.preprocessing import normalize, scale, MultiLabelBinarizer
from sklearn.metrics import f1_score
from sklearn.multiclass import OneVsRestClassifier
from sklearn.svm import LinearSVC
from sklearn.linear_model import LogisticRegression
from sklearn.linear_model import LogisticRegressionCV
In [2]:
# Load weight
with open('../src/mane/prototype/embeddings/BC3047.weights', 'rb') as f:
w = p.load(f)
# Load graph
bc = g.graph_from_pickle('../src/mane/data/blogcatalog3.graph',
'../src/mane/data/blogcatalog3.community')
In [3]:
emb = (w[0] + w[1]) / 2
emb = normalize(emb)
In [4]:
x_train, yl_train, x_test, yl_test = bc.get_ids_labels(0.5)
In [5]:
lg = OneVsRestClassifier(LogisticRegression())
In [6]:
X_train = [emb[i] for i in x_train]
Y_train = MultiLabelBinarizer(classes=range(1,40)).fit_transform(yl_train)
In [7]:
lg.fit(X_train, Y_train)
Out[7]:
In [8]:
lg.predict_proba(emb[9566].reshape(1,-1)).shape
Out[8]:
In [9]:
X_test = [emb[i] for i in x_test]
Y_test = MultiLabelBinarizer(classes=range(1,40)).fit_transform(yl_test)
In [10]:
pred = lg.predict_proba([i for i in X_test])
In [11]:
Y_test
Out[11]:
In [35]:
pred[0].argsort()[-3:]
Out[35]:
In [12]:
len(pred)
Out[12]:
In [13]:
len(yl_test)
Out[13]:
In [14]:
pred[0]
Out[14]:
In [15]:
yl_test[0]
Out[15]:
In [16]:
pred[0].argsort()[-1:]
Out[16]:
In [17]:
num_pred = []
In [18]:
for i, j in enumerate(pred):
k = len(yl_test[i])
num_pred.append(j.argsort()[-k:])
In [19]:
len(num_pred)
Out[19]:
In [20]:
num_pred[0]
Out[20]:
In [21]:
num_pred[1]
Out[21]:
In [22]:
Y_pred = MultiLabelBinarizer(classes=range(1,40)).fit_transform(num_pred)
In [23]:
Y_pred[0]
Out[23]:
In [24]:
Y_test[0]
Out[24]:
In [25]:
for i, j in enumerate(num_pred):
num_pred[i] = [k+1 for k in j]
In [26]:
num_pred[0]
Out[26]:
In [27]:
yl_test[0]
Out[27]:
In [28]:
Y_pred = MultiLabelBinarizer(classes=range(1,40)).fit_transform(num_pred)
In [29]:
Y_pred[0]
Out[29]:
In [30]:
Y_test[0]
Out[30]:
In [31]:
f1_score(y_pred=Y_pred, y_true=Y_test, average='macro')
Out[31]:
In [32]:
f1_score(y_pred=Y_pred, y_true=Y_test, average='micro')
Out[32]:
In [33]:
f1_score(y_pred=Y_pred, y_true=Y_test, average='macro')
Out[33]:
In [ ]: